﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Common.UIFunction
{
    public class TextTable
    {
        DataGridView DGV;
        int ColumnCount;
        public TextTable(DataGridView dgv)
        {
            this.DGV = dgv;
            this.ColumnCount = 2;
            this.InitDGV();
        }

        public TextTable(DataGridView dgv, int count)
        {
            this.DGV = dgv;
            this.ColumnCount = count;
            this.InitDGV();
        }

        public void InitDGV()
        {
            DGV.BackgroundColor = System.Drawing.Color.White;
            DGV.AllowUserToAddRows = false;
            DGV.RowHeadersVisible = false;
            DGV.ColumnHeadersVisible = false;
            DGV.AllowUserToResizeColumns = false;
            DGV.AllowUserToResizeRows = false;
        }
        public void TextTableBound(string[,] context, int[] width)
        {
            if (context.GetLongLength(1) != width.Length || width.Length != this.ColumnCount)
                return;

            DGV.Columns.Clear();
            for (int i = 0; i < this.ColumnCount; i++)
            {
                DGV.Columns.Add("","");
                DGV.Columns[i].Width = width[i];
                DGV.Columns[i].ReadOnly = true;
            }

            DGV.Rows.Clear();
            long rowcount = context.GetLongLength(0);
            DGV.Rows.Add(Convert.ToInt32(rowcount));
           
            this.ResizeDGV();
            for (int rowindex = 0; rowindex < DGV.Rows.Count; rowindex++)
            {
                for (int columnindex = 0; columnindex < DGV.Columns.Count; columnindex++)
                {
                    if(context[rowindex, columnindex] != null)
                        DGV.Rows[rowindex].Cells[columnindex].Value = context[rowindex, columnindex];
                }
            }
        }

        public void ResizeDGV()
        {
            DGV.Width = this.DGV.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + 3;
            DGV.Height = this.DGV.Rows.GetRowsHeight(DataGridViewElementStates.Visible) + 3;
        }
        

    }
}
